package com.app.view;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.CharacterStyle;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.app.util.Utils;

public class LinkTextView extends TextView{

//	private Paint mPaint = new Paint();
	private String mStr ="";
	private Rect mTextBound = new Rect();
	private boolean bShow = false;
	
	
	public LinkTextView(Context context){
		super(context);
		init();
	}
	
	public LinkTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
		
	     
	}
	
	/*
	public Rect getRect(){	
		return mTextBound;
	}
	*/

	///*
	public void set_str(String str){
		mStr = str;
		setText(str);
		
	}
	//*/
	
	
	private void init(){
//		mPaint.setColor(Color.BLACK);
//		mPaint.setTextSize(13);
		
		mStr = this.getText().toString();
		
		System.out.println("=====init: "+mStr);
	}
	
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
	//	mStr = getText().toString();
		
		if(mStr.isEmpty() == false)
		{
			
	        Pattern p = Pattern.compile("[0-9]+");
	        Matcher matcher = p.matcher(mStr);
	        while(matcher.find()){
	        	int count = matcher.groupCount();
	        	String str = matcher.group();
	        	System.out.println("======"+str);
	        	
	        	int start_index = mStr.indexOf(str);
	        	fuc( mStr, start_index, str );
	        	
	        }
		}
        // 
        mStr = "";
        
	}
	
	void fuc( String str, int start_index, String str_sub){
		
		
		// 判断是否为手机号码，如果是将可拨打
		if(Utils.isMobileNO(str_sub)
				|| Utils.isTelNO(str_sub) )
        {
			System.out.println("======spanable: "+str_sub);
            
        	CharacterStyle style_underline = new UnderlineSpan();
        	CharacterStyle style_forgound_color = new ForegroundColorSpan(Color.parseColor("#33B5E5"));
        	SpannableString spanable = new SpannableString(str);
        	CharacterStyle style_click = new MyClickableSpan( new Intent(), str_sub );
        	spanable.setSpan(style_underline, start_index, start_index+str_sub.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        	spanable.setSpan(style_forgound_color, start_index, start_index+str_sub.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        	spanable.setSpan(style_click, start_index, start_index+str_sub.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        	setText(spanable);
        	setMovementMethod(LinkMovementMethod.getInstance());
        }  
        
        
	}
	
		

	 class MyClickableSpan extends ClickableSpan  {
	
		 Intent intent;
		 String str;
		public MyClickableSpan(Intent intent, String str){
			this.intent = intent;
			this.str = str;
		}
		
	
	    @Override
	    public void onClick(View widget) {
	    	
	    	System.out.println("LinkTextView onClick.");
	    	
	        Context context = widget.getContext();
	//        context.startActivity(intent);
	        
	        
	        Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
					+ str));
			context.startActivity(i);
			
	        
	        //Utils1.showToastShort(context, "111111111111");
	    }
	    
	    
	
	};
	
}
